﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using PS.WSSDeploy.Contract;

namespace PS.WSSDeploy.Service.Tools
{
#if SP2010
    public class SandboxSolutionsManager
    {
        private const string SolutionsFolderUrl = "_catalogs/solutions";

        private string SiteUrl { get; set;}

        public SandboxSolutionsManager(string siteUrl)
        {
            SiteUrl = siteUrl;
        }

        public void Add(WSSFile wsp)
        {
            using (var site = new SPSite(SiteUrl))
            {
                using (var rootweb = site.RootWeb)
                {
                    var fileCollection = rootweb.GetFolder(SolutionsFolderUrl).Files;
                    if(fileCollection != null)
                    {
                        fileCollection.Add(wsp.Name, wsp.Content);
                        rootweb.Update();
                    }
                }
            }
        }

        public void Remove(string wspName)
        {
            using (var site = new SPSite(SiteUrl))
            {
                using (var rootweb = site.RootWeb)
                {
                    var fileCollection = rootweb.GetFolder(SolutionsFolderUrl).Files;
                    if (fileCollection != null)
                    {
                        fileCollection.Delete(wspName);
                        rootweb.Update();
                    }
                }
            }          
        }

        public bool IsDeployed(string wspName)
        {
            using (var site = new SPSite(SiteUrl))
            {
                using (var rootweb = site.RootWeb)
                {
                    if (rootweb.Site.Solutions.Cast<SPUserSolution>().Any(solution => solution.Name.Equals(wspName)))
                    {
                        return true;
                    }
                }
            }
            return false;
        }
        
        public bool IsExists(string wspName)
        {
            using (var site = new SPSite(SiteUrl))
            {
                using (var rootweb = site.RootWeb)
                {
                    var solGallery = rootweb.Site.GetCatalog(SPListTemplateType.SolutionCatalog);
                    if (solGallery.Items.Cast<SPListItem>().Any(item => item.File.Name.Equals(wspName)))
                    {
                        return true;
                    }
                }
            }

            return false;
        }

        public void Activate(string wspName)
        {
            using (var site = new SPSite(SiteUrl))
            {
                using (var rootweb = site.RootWeb)
                {
                    var solGallery = rootweb.Site.GetCatalog(SPListTemplateType.SolutionCatalog);
                    foreach (SPListItem item in solGallery.Items)
                    {
                        if (item.File.Name.Equals(wspName))
                        {
                            SPUserSolution solutiontoActivate = rootweb.Site.Solutions.Add(item.ID);
                            EnsureSiteCollectionFeaturesActivated(solutiontoActivate, site);
                            rootweb.Update();
                            break;
                        }
                    }
                }
            }
        }

        public void Deactivate(string wspName)
        {
            using (var site = new SPSite(SiteUrl))
            {
                using (var rootweb = site.RootWeb)
                {
                    foreach (SPUserSolution solution in rootweb.Site.Solutions)
                    {
                        if (solution.Name.Equals(wspName))
                        {
                            rootweb.Site.Solutions.Remove(solution);
                            rootweb.Update();
                            break;
                        }
                    }
                }
            }
        }

        private static void EnsureSiteCollectionFeaturesActivated(SPUserSolution solution, SPSite site)
        {
            IEnumerable<SPFeatureDefinition> oListofFeatures = GetFeatureDefinitionsInSolution(solution, site);
            foreach (SPFeatureDefinition def in oListofFeatures)
            {
                if (((def.Scope == SPFeatureScope.Site) && def.ActivateOnDefault) && (site.Features[def.Id] == null))
                {
                    site.Features.Add(def.Id, false, SPFeatureDefinitionScope.Site);
                }
            }
        }

        private static IEnumerable<SPFeatureDefinition> GetFeatureDefinitionsInSolution(SPUserSolution solution, SPSite site)
        {
            return site.FeatureDefinitions.Where(definition => definition.SolutionId.Equals(solution.SolutionId)).ToList();
        }
    }
#endif
}
